<#assign ctx="${rc.contextPath}">
<html>
<head>
    <title>新增/编辑用户信息</title>
    <script type="text/javascript">
        $(document).ready(function () {
            var accUserId = '${accUserId!}';
            if (accUserId != '') {
                $.post("${ctx}/account/user/selectId", {accUserId: accUserId}, function (data) {
                    var data = JSON.parse(data);
                    var roleList = data.roleList;
                    var accDepartment = data.accDepartment;
                    var roleIds = new Array();
                    for (var i = 0; i < roleList.length; i++) {
                        if (roleList[i] != null) {
                            roleIds[i] = roleList[i].accRoleId;
                        }
                    }
                    $("#accUserName").val(data.accUserName);
                    $("#accUserId").val(data.accUserId);
                    $("#accRoleId").combobox('setValues', roleIds);
                    $("#accDepartmentId").combobox('setValue', accDepartment.accDepartmentId);
                });
            }

            $('#accRoleId').combobox({
                url: '${ctx}/account/role/list',
                valueField: 'accRoleId',
                textField: 'accRoleName',
                multiple: true,
                editable: false
            });

            $('#accDepartmentId').combobox({
                url: '${ctx}/account/department/list',
                valueField: 'accDepartmentId',
                textField: 'accDepartmentName',
                editable: false
            });

        });

        function save() {
            if ($("#frmEdit").form('validate') && checkForm("frmEdit")) {
                var data = $("#frmEdit").serializeObject();
                var accRoleIds = data.accRoleIds;
                if (jQuery.isArray(accRoleIds)) {
                    var roles = "";
                    for (var i = 0; i < accRoleIds.length; i++) {
                        if (accRoleIds[i] != "") {
                            roles += accRoleIds[i] + ",";
                        }
                    }
                    data.accRoleIds = roles;
                }
                console.log(data.accRoleIds);
                $.ajax({
                    type: "POST",
                    url: $("#frmEdit").attr('action'),
                    data: data,
                    dataType: 'json',
                    success: function (result) {
                        successMsg(result.message);
                        if (result.status == 200) {
                            window.location.href = "${ctx}/account/user/page/";
                        }
                    }
                });
            }
        }

        function cancel() {
            window.location.href = "${ctx}/account/user/page/";
        }

        function checkName() {
            var name = $('#accUserName').val();
            var id = $('#accUserId').val();
            if (name == "") {
                $.messager.alert("提示", "名称不能为空", "warning")
                return;
            }
            $.ajax({
                type: "POST",
                url: '${ctx}/account/user/checkName',
                data: {'accUserName': name, 'id': id},
                dataType: 'json',
                success: function (result) {
                    successMsg(result.message);
                }
            });
        }
    </script>
</head>
<body>
<div class="breadcrumb"> 用户设置 > 用户管理 > 新增/编辑</div>

<div class="toolbar">
    <input type="button" class="btn btn-primary" onclick="save()" value="保存">
    <input type="button" class="btn" onclick="cancel()" value="取消">
</div>

<form class="form-horizontal form-edit" id="frmEdit"  action="${ctx}/account/user/save">
    <fieldset class="dg-fieldset">
        <input type="hidden" id="accUserId" name="accUserId" value="newUser"/>
        <legend>用户信息</legend>
        <div class="form-item">
            <div class="form-lab">用户名：<span class="must">*</span></div>
            <div class="form-field">
                <input class="easyui-validatebox text" name="accUserName" id="accUserName"
                       data-options="required:true" maxlength="5"/>
                <a href="#" onclick="checkName()">&nbsp;检测用户名是否可用</a>
            </div>
        </div>
    <#if !accUserId??>
        <div class="form-item">
            <div class="form-lab">密码：<span class="must">*</span></div>
            <div class="form-field">
                <input class="easyui-validatebox text" type="password" name="accUserPassword"
                       id="accUserPassword"
                       data-options="required:true" validType="length[4,32]"/>
            </div>
        </div>
        <div class="form-item">
            <div class="form-lab">再次输入密码：<span class="must">*</span></div>
            <div class="form-field">
                <input class="easyui-validatebox text" type="password" name="repassword" id="repassword"
                       required="true" validType="equalTo['#accUserPassword']" invalidMessage="两次输入密码不匹配"/>
            </div>
        </div>
    </#if>
        <div class="form-item">
            <div class="form-lab">用户角色：<span class="must">*</span></div>
            <div class="form-field">
                <select id="accRoleId" name="accRoleIds" class="easyui-combobox" required="true">
                </select>
            </div>
        </div>
        <div class="form-item" id="showType">
            <div class="form-lab">用户群组：<span class="must">*</span></div>
            <div class="form-field">
                <select id="accDepartmentId" name="accDepartmentId" class="easyui-combobox" required="true">
                </select>
            </div>
        </div>
    </fieldset>
</form>

</body>
</html>